/*
 * @lc app=leetcode.cn id=404 lang=cpp
 *
 * [404] 左叶子之和
 *
 * https://leetcode.cn/problems/sum-of-left-leaves/description/
 *
 * algorithms
 * Easy (62.58%)
 * Likes:    697
 * Dislikes: 0
 * Total Accepted:    280.4K
 * Total Submissions: 447.9K
 * Testcase Example:  '[3,9,20,null,null,15,7]'
 *
 * 给定二叉树的根节点 root ，返回所有左叶子之和。
 * 
 * 
 * 
 * 示例 1：
 * 
 * 
 * 
 * 
 * 输入: root = [3,9,20,null,null,15,7] 
 * 输出: 24 
 * 解释: 在这个二叉树中，有两个左叶子，分别是 9 和 15，所以返回 24
 * 
 * 
 * 示例 2:
 * 
 * 
 * 输入: root = [1]
 * 输出: 0
 * 
 * 
 * 
 * 
 * 提示:
 * 
 * 
 * 节点数在 [1, 1000] 范围内
 * -1000 <= Node.val <= 1000
 * 
 * 
 * 
 * 
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int LeftLeaves(TreeNode* node, bool isLeft) {
        //当前节点为空, 或者当前节点并非左叶子节点
        if (node == nullptr || 
            (node->left == nullptr && node->right == nullptr && !isLeft))
            return 0;
        
        //左叶子节点
        if (node->left == nullptr && node->right == nullptr)
            return node->val;

        //得到左子树和右子树的左叶子节点的结果
        int left = LeftLeaves(node->left, true);
        int right = LeftLeaves(node->right, false);

        return left + right;    //相加
    }

    int sumOfLeftLeaves(TreeNode* root) {
        return LeftLeaves(root, false);
    }
};
// @lc code=end

